﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class Business_Business_Do : System.Web.UI.Page
{
    protected int NewsKey
    {
        get { return string.IsNullOrEmpty(Request["NewsKey"]) ? 0 : Convert.ToInt32(Request["NewsKey"]); }
    }

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["MenuType"]) ? "" : Request["MenuType"].ToString(); }
    }

    protected Dat_News news;

    protected void Page_Load(object sender, EventArgs e)
    {
        news = new News().SelectNews(NewsKey);

        if (!this.IsPostBack)
        {

            //---------------------------按状态显示----------------------
            
            ViewState["ClsID"] =news.ClsID;

            if (news.ClsID.Contains("6"))    //报价显示
            {
                Quote_StockType.Value = news.Quote_StockType;
                Quote_StockID.Value = WebUtility.IntToInt(news.Quote_StockID).ToString();
                Quote_StockName.Text = News.GetQuoteContent(news.Quote_StockType, Convert.ToInt32(news.Quote_StockID));

                Quote_IsQuote.SelectedValue = news.Quote_IsQuote;
                if (Quote_IsQuote.SelectedValue == "已报价")
                {
                    LB_Quote_Price.Visible = true;
                    Quote_Price.Visible = true;
                    Quote_Price.Text = WebUtility.DecimalToString(news.Quote_Price);
                }
                else
                {
                    LB_Quote_Price.Visible = false;
                    Quote_Price.Visible = false;
                }

            }

            if (news.Type == 3 || news.Type == 4 || news.Type == 5 || news.Type == 6)
            {
                Status.SelectedValue = news.Type.ToString();
            }

            //预约处理时间
            TechOneD.Attributes.Add("onfocus", "selectDate(this)");
            TechTwoD.Attributes.Add("onfocus", "selectDate(this)");
            string H, M;
            if (WebUtility.DateTimeToString(news.TechOneTime) != "")
            {
                TechOneD.Text = WebUtility.DateTimeToString(news.TechOneTime).Split(' ')[0];
                H = WebUtility.DateTimeToString(news.TechOneTime).Split(' ')[1].Split(':')[0];
                if (H.Length == 1) H = "0" + H;
                TechOneT_H.Text = H;
                M = WebUtility.DateTimeToString(news.TechOneTime).Split(' ')[1].Split(':')[1];
                if (M.Length == 1) M = "0" + M;
                TechOneT_M.Text = M;
            }
            if (WebUtility.DateTimeToString(news.TechTwoTime) != "")
            {
                TechTwoD.Text = WebUtility.DateTimeToString(news.TechTwoTime).Split(' ')[0];
                H = WebUtility.DateTimeToString(news.TechTwoTime).Split(' ')[1].Split(':')[0];
                if (H.Length == 1) H = "0" + H;
                TechTwoT_H.Text = H;
                M = WebUtility.DateTimeToString(news.TechTwoTime).Split(' ')[1].Split(':')[1];
                if (M.Length == 1) M = "0" + M;
                TechTwoT_M.Text = M;
            }

            if (news.Type == 5 || news.Type == 6)
            {
                tb_Do.Visible = true;

                if (WebUtility.DateTimeToString(news.TechFiveTime) != "")
                {   
                    TechFiveTimeD.Text = WebUtility.DateTimeToString(news.TechFiveTime).Split(' ')[0];
                    H = WebUtility.DateTimeToString(news.TechFiveTime).Split(' ')[1].Split(':')[0];
                    if (H.Length == 1) H = "0" + H;
                    TechFiveTimeT_H.Text = H;
                    M = WebUtility.DateTimeToString(news.TechFiveTime).Split(' ')[1].Split(':')[1];
                    if (M.Length == 1) M = "0" + M;
                    TechFiveTimeT_M.Text = M;
                }

                if (WebUtility.DateTimeToString(news.TechSixTime) != "")
                {
                    TechSixTimeD.Text = WebUtility.DateTimeToString(news.TechSixTime).Split(' ')[0];
                    H = WebUtility.DateTimeToString(news.TechSixTime).Split(' ')[1].Split(':')[0];
                    if (H.Length == 1) H = "0" + H;
                    TechSixTimeT_H.Text = H;
                    M = WebUtility.DateTimeToString(news.TechSixTime).Split(' ')[1].Split(':')[1];
                    if (M.Length == 1) M = "0" + M;
                    TechSixTimeT_M.Text = M;
                }

                Content_Tech_Online.Text = news.Content_Tech_Online;
                Content_Tech.Text = news.Content_Tech;
                Content_Tech_Result.Text = news.Content_Tech_Result;
              
            }

            //---------------------------按用户类型显示----------------------

            if (Convert.ToInt32(Session["UserType"]) == 2 || Convert.ToInt32(Session["UserType"]) == 4)
            {
                txt_HuiDan.SelectedValue = news.HuiDan;
                txt_JTfee.Text = WebUtility.DecimalToString(news.JTfee);
                txt_ServiceNum.SelectedValue = news.ServiceNum.ToString();
            }
            if (Convert.ToInt32(Session["UserType"]) == 4)
            {
                txt_ServiceNum.SelectedValue = news.ServiceNum.ToString();
            }
        }

        TechFiveTimeD.Attributes.Add("onfocus", "selectDate(this)");
        TechFiveTimeD.Attributes.Add("onpropertychange", "setDefaultDate(this)");
        TechSixTimeD.Attributes.Add("onfocus", "selectDate(this)");
    }

    //修改
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Dat_News news = null, news_price = null;

        decimal? de = null;

        switch (Convert.ToInt32(Status.SelectedValue))
        {
            case 3:
            case 4:
                if (Convert.ToInt32(Session["UserType"]) == 2 || Convert.ToInt32(Session["UserType"]) == 4)
                {
                    news = new Dat_News
                    {
                        NewsKey = NewsKey,
                        Type = Convert.ToInt32(Status.SelectedValue),
                        TechOneTime = Convert.ToDateTime(TechOneD.Text.Trim() + " " + TechOneT_H.Text + ":" + TechOneT_M.Text + ":00"),
                        TechTwoTime = Convert.ToDateTime(TechTwoD.Text.Trim() + " " + TechTwoT_H.Text + ":" + TechTwoT_M.Text + ":00"),
                        UpdateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"]),
                        HuiDan = txt_HuiDan.SelectedValue,
                        JTfee = string.IsNullOrEmpty(txt_JTfee.Text) ? de : Convert.ToDecimal(txt_JTfee.Text),
                        ServiceNum = Convert.ToInt32(txt_ServiceNum.SelectedValue)
                    };
                }
                else   //工程师
                {
                    news = new Dat_News
                    {
                        NewsKey = NewsKey,
                        Type = Convert.ToInt32(Status.SelectedValue),
                        TechOneTime = Convert.ToDateTime(TechOneD.Text.Trim() + " " + TechOneT_H.Text + ":" + TechOneT_M.Text + ":00"),
                        TechTwoTime = Convert.ToDateTime(TechTwoD.Text.Trim() + " " + TechTwoT_H.Text + ":" + TechTwoT_M.Text + ":00"),
                        UpdateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"])
                    };
                }
                break;
            case 5:
                if (Convert.ToInt32(Session["UserType"]) == 2 || Convert.ToInt32(Session["UserType"]) == 4)
                {
                    news = new Dat_News
                    {
                        NewsKey = NewsKey,
                        RootID = NewsKey,
                        Type = 5,
                        TechOneTime = Convert.ToDateTime(TechOneD.Text.Trim() + " " + TechOneT_H.Text + ":" + TechOneT_M.Text + ":00"),
                        TechTwoTime = Convert.ToDateTime(TechTwoD.Text.Trim() + " " + TechTwoT_H.Text + ":" + TechTwoT_M.Text + ":00"),
                        TechFiveTime = Convert.ToDateTime(TechFiveTimeD.Text.Trim() + " " + TechFiveTimeT_H.Text + ":" + TechFiveTimeT_M.Text + ":00"),
                        TechSixTime = Convert.ToDateTime(TechSixTimeD.Text.Trim() + " " + TechSixTimeT_H.Text + ":" + TechSixTimeT_M.Text + ":00"),
                        Content_Tech_Online = Content_Tech_Online.Text.Trim(),
                        Content_Tech = Content_Tech.Text.Trim(),
                        Content_Tech_Result = Content_Tech_Result.Text.Trim(),
                        FinishTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"]),
                        HuiDan = txt_HuiDan.SelectedValue,
                        JTfee = string.IsNullOrEmpty(txt_JTfee.Text) ? de : Convert.ToDecimal(txt_JTfee.Text),
                        ServiceNum = Convert.ToInt32(txt_ServiceNum.SelectedValue)
                    };
                }
                else  //工程师
                {
                    news = new Dat_News
                    {
                        NewsKey = NewsKey,
                        RootID = NewsKey,
                        Type = 5,
                        TechOneTime = Convert.ToDateTime(TechOneD.Text.Trim() + " " + TechOneT_H.Text + ":" + TechOneT_M.Text + ":00"),
                        TechTwoTime = Convert.ToDateTime(TechTwoD.Text.Trim() + " " + TechTwoT_H.Text + ":" + TechTwoT_M.Text + ":00"),
                        TechFiveTime = Convert.ToDateTime(TechFiveTimeD.Text.Trim() + " " + TechFiveTimeT_H.Text + ":" + TechFiveTimeT_M.Text + ":00"),
                        TechSixTime = Convert.ToDateTime(TechSixTimeD.Text.Trim() + " " + TechSixTimeT_H.Text + ":" + TechSixTimeT_M.Text + ":00"),
                        Content_Tech_Online = Content_Tech_Online.Text.Trim(),
                        Content_Tech = Content_Tech.Text.Trim(),
                        Content_Tech_Result = Content_Tech_Result.Text.Trim(),
                        FinishTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"])
                    };
                }
                break;
            case 6:
                if (Convert.ToInt32(Session["UserType"]) == 2 || Convert.ToInt32(Session["UserType"]) == 4)
                {
                    news = new Dat_News
                    {
                        NewsKey = NewsKey,
                        Type = 6,
                        TechOneTime = Convert.ToDateTime(TechOneD.Text.Trim() + " " + TechOneT_H.Text + ":" + TechOneT_M.Text + ":00"),
                        TechTwoTime = Convert.ToDateTime(TechTwoD.Text.Trim() + " " + TechTwoT_H.Text + ":" + TechTwoT_M.Text + ":00"),
                        TechFiveTime = Convert.ToDateTime(TechFiveTimeD.Text.Trim() + " " + TechFiveTimeT_H.Text + ":" + TechFiveTimeT_M.Text + ":00"),
                        TechSixTime = Convert.ToDateTime(TechSixTimeD.Text.Trim() + " " + TechSixTimeT_H.Text + ":" + TechSixTimeT_M.Text + ":00"),
                        Content_Tech_Online = Content_Tech_Online.Text.Trim(),
                        Content_Tech = Content_Tech.Text.Trim(),
                        Content_Tech_Result = Content_Tech_Result.Text.Trim(),
                        FinishTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"]),
                        HuiDan = txt_HuiDan.SelectedValue,
                        JTfee = string.IsNullOrEmpty(txt_JTfee.Text) ? de : Convert.ToDecimal(txt_JTfee.Text),
                        ServiceNum = Convert.ToInt32(txt_ServiceNum.SelectedValue)
                    };
                }
                else  //工程师
                {
                    news = new Dat_News
                    {
                        NewsKey = NewsKey,
                        Type = 6,
                        TechOneTime = Convert.ToDateTime(TechOneD.Text.Trim() + " " + TechOneT_H.Text + ":" + TechOneT_M.Text + ":00"),
                        TechTwoTime = Convert.ToDateTime(TechTwoD.Text.Trim() + " " + TechTwoT_H.Text + ":" + TechTwoT_M.Text + ":00"),
                        TechFiveTime = Convert.ToDateTime(TechFiveTimeD.Text.Trim() + " " + TechFiveTimeT_H.Text + ":" + TechFiveTimeT_M.Text + ":00"),
                        TechSixTime = Convert.ToDateTime(TechSixTimeD.Text.Trim() + " " + TechSixTimeT_H.Text + ":" + TechSixTimeT_M.Text + ":00"),
                        Content_Tech_Online = Content_Tech_Online.Text.Trim(),
                        Content_Tech = Content_Tech.Text.Trim(),
                        Content_Tech_Result = Content_Tech_Result.Text.Trim(),
                        FinishTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"])
                    };
                }
                break;
        }

        if ( ViewState["ClsID"].ToString().Contains("6") )   //报价修改
        {
            news_price = new Dat_News
            {
                NewsKey = NewsKey,
                Quote_StockType = Quote_StockType.Value,
                Quote_StockID = Convert.ToInt32(Quote_StockID.Value),
                Quote_IsQuote = Quote_IsQuote.SelectedValue,
                Quote_Price = (Quote_Price.Text.Trim() == "" || Quote_IsQuote.SelectedValue == "未报价") ? de : Convert.ToDecimal(Quote_Price.Text.Trim())
            };
        }

        if (new News().EditNews_Do(news) && new News().EditNews_Do_Price(news_price))
        {
            if (MenuType == "121")   //未完成界面
                WebUtility.JsAlertToParent("任务处理成功", "Business_NotFinish.aspx?menutype=121");
            else if (MenuType == "122")  //已完成界面
                WebUtility.JsAlertToParent("任务处理成功", "Business_Finish.aspx?menutype=122&type=6");
            else   //任务处理界面
                WebUtility.JsAlertToParent("任务处理成功", "Business.aspx?menutype=133&type=1,2,4,5");
        }
    }

    //
    protected void Status_TextChanged(object sender, EventArgs e)
    {
        if (Status.SelectedValue == "3" || Status.SelectedValue == "4")
            tb_Do.Visible = false;
        else
            tb_Do.Visible = true;
    }

     //报价切换
    protected void Quote_IsQuote_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Quote_IsQuote.SelectedValue == "已报价")
        {
            LB_Quote_Price.Visible = true;
            Quote_Price.Visible = true;
        }
        else
        {
            LB_Quote_Price.Visible = false;
            Quote_Price.Visible = false;
        }
    }
}
